package com.example.design.ordermanagement.statemachine;

import com.example.design.ordermanagement.state.OrderState;
import com.example.design.ordermanagement.state.OrderStateChangeAction;
import com.example.design.utils.RedisUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.statemachine.config.EnableStateMachine;
import org.springframework.statemachine.config.StateMachineConfigurerAdapter;
import org.springframework.statemachine.config.builders.StateMachineStateConfigurer;
import org.springframework.statemachine.config.builders.StateMachineTransitionConfigurer;
import org.springframework.statemachine.persist.RepositoryStateMachinePersist;
import org.springframework.statemachine.redis.RedisStateMachineContextRepository;
import org.springframework.statemachine.redis.RedisStateMachinePersister;

import javax.annotation.Resource;
import java.util.EnumSet;

//Spring状态机配置类
@EnableStateMachine(name = "orderStateMachine")
@Configuration
public class OrderStateMachineConfig extends StateMachineConfigurerAdapter<OrderState, OrderStateChangeAction> {

    @Resource
    private RedisUtils redisUtils;

    @Resource
    private RedisConnectionFactory redisConnectionFactory;

    //覆盖配置，进行状态机的初始化
    @Override
    public void configure(StateMachineStateConfigurer<OrderState, OrderStateChangeAction> states) throws Exception {
        states.withStates()
                //设置订单创建成功后初始化状态为待支付
                .initial(OrderState.ORDER_WAIT_PAY)
                .states(EnumSet.allOf(OrderState.class));
    }


    @Override
    public void configure(StateMachineTransitionConfigurer<OrderState, OrderStateChangeAction> transitions) throws Exception {
        //从 ORDER_WAIT_PAY 状态，触发 PAY_ORDER 事件，转移到 ORDER_WAIT_RECEIVE 状态
        transitions
                .withExternal()
                .source(OrderState.ORDER_WAIT_PAY)
                .target(OrderState.ORDER_WAIT_SEND)
                .event(OrderStateChangeAction.PAY_ORDER)
                .and()
                //从 ORDER_WAIT_SEND 状态，触发 SEND_ORDER 事件，转移到 ORDER_WAIT_RECEIVE 状态
                .withExternal().source(OrderState.ORDER_WAIT_SEND)
                .target(OrderState.ORDER_WAIT_RECEIVE)
                .event(OrderStateChangeAction.SEND_ORDER)
                .and()
                //从 ORDER_WAIT_RECEIVE 状态，触发 RECEIVE_ORDER 事件，转移到 ORDER_FINISH 状态
                .withExternal().source(OrderState.ORDER_WAIT_RECEIVE)
                .target(OrderState.ORDER_FINISH)
                .event(OrderStateChangeAction.RECEIVE_ORDER);
    }

    @Bean(name = "stateMachineRedisPersister")
    public RedisStateMachinePersister<OrderState, OrderStateChangeAction> getRedisPersister() {
        RedisStateMachineContextRepository<OrderState, OrderStateChangeAction> repository = new RedisStateMachineContextRepository(redisConnectionFactory);
        RepositoryStateMachinePersist<OrderState, OrderStateChangeAction> persister = new RepositoryStateMachinePersist<>(repository);
        return new RedisStateMachinePersister<>(persister);
    }
}
